#include "HitRecord.h"

//If multiple hits occur at the same point, merge them together and average their normals
void HitRecord::mergeHits() {
	hits.sort();

	int count = 0;
	Hit* cur = &*(hits.begin()++);
	
	std::list<Hit>::iterator it = (hits.begin())++ ++;
	while(it != hits.end()){
		if(it->t < cur->t + 0.01 && it->t > cur->t - 0.01){
			cur->n = cur->n * count + it->n;
			cur->n.normalize();
			count++;
			std::list<Hit>::iterator r = it;
			it++;
			hits.erase(r);
		}
		else {
			cur = &(*it);
			if(count == 0)
				count = 1;
			it++;
		}
	}
}